package com.example.demo.util;

import com.example.demo.util.httpclient.RestTemplateUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.Iterator;
import java.util.Map;

public class SwaggerUtils {

    public static void main(String[] args) {
        //AccountLoginStep accountLoginStep = new AccountLoginStep();
        //accountLoginStep.login();
        String getAllRequest = "https://ofp-account-admin.docker-ofp-prd.lefile.cn/v2/api-docs?group=Profile";//Admin
        RestTemplateUtils restTemplateUtils = new RestTemplateUtils();
        String dev = restTemplateUtils.get(getAllRequest, "dev");
        //System.out.println(dev);
        Gson gson = new Gson();

        JsonObject jsonObject = gson.fromJson(dev,JsonObject.class);
        JsonObject paths = jsonObject.get("paths").getAsJsonObject();
        Iterator<Map.Entry<String, JsonElement>> iterator = paths.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, JsonElement> next = iterator.next();
            JsonObject asJsonObject = next.getValue().getAsJsonObject();
            Map.Entry<String, JsonElement> next1 = asJsonObject.entrySet().iterator().next();
            String requestType = next1.getKey();
            JsonElement parameters = next1.getValue().getAsJsonObject().get("parameters");
            JsonElement description = next1.getValue().getAsJsonObject().get("description");
            if("get".equalsIgnoreCase(requestType)){
                StringBuilder parametersURL = new StringBuilder("");
                if(parameters!=null){
                    parametersURL.append("?");
                    JsonArray asJsonArray = parameters.getAsJsonArray();
                    asJsonArray.forEach(x->{
                        JsonObject asJsonObject1 = x.getAsJsonObject();
                        String name = asJsonObject1.get("name").getAsString();
                        boolean required = asJsonObject1.get("required").getAsBoolean();
                        if(required){
                            parametersURL.append(name).append("={{$").append(name).append("}}&");
                        }
                    });
                    parametersURL.deleteCharAt(parametersURL.length()-1);
                }
                System.out.println("");
                System.out.println("### "+description);
                System.out.println(requestType.toUpperCase() + " https://{{host}}"+next.getKey()+ parametersURL);
            }else if("post".equalsIgnoreCase(requestType)){

                //System.out.println("### "+requestType.toUpperCase());
                //System.out.println(requestType.toUpperCase() + " "+next.getKey());
                //System.out.println("Content-Type: application/json");
            }



        }
    }
}
